#!/usr/bin/env python3

import sys
import datetime
import os
import subprocess
import re  # 正则
import os.path

# 相对目录结构如下,脚本设计依赖于目录结构安排
#     或者更改全局变量也可以运行，这样需要对脚本熟悉，以便方便更改
# .
# ├── code_tool_nio
# ├── cosine_perception
# ├── localization
# └── packages

# sud_all_ver=sys.argv[1]
# src_localization_repo_dir=sys.argv[2]
# pakcages_root_dir=sys.argv[3]

print("\n")
print("-----------------start------------------------")
print("run in docker!!!!!!!!")
print("compile localization_module to generate release files")
print("compile cosine_perception to do compile test")
print("\n\n")

ws_root="/code"
src_localization_repo_dir=ws_root+"/localization"
cosine_perception_repo_dir=ws_root+"/cosine_perception"

arch_type="aarch64"
release_types = list()
release_types.append("release")
release_types.append("debug")

build_sh_file = open(cosine_perception_repo_dir+"/build.sh","r")

# -------------------先运行脚本拉取资源，然后注释拉取资源，利用本地 localization_module的文件继续编译-------------------------------
build_sh_text=build_sh_file.read()
find_res=re.findall("        source deps_setup.sh \$\@",build_sh_text)
if len(find_res) != 0:
  print("find source deps_setup.sh $@")

  #这句话主要是拉取资源，编译肯定会失败，
  #领取完毕资源后，就把拉取资源的语句注释，然后把最新的localization_module 内容 更新到 cosine_perception 当中，在编译就ok了
  os.system('cd %s && ./build.sh make aarch64 release'%(cosine_perception_repo_dir)) 

  build_sh_text_new = build_sh_text.replace("        source deps_setup.sh $@","        # source deps_setup.sh $@")
  with open(cosine_perception_repo_dir+"/build.sh","w") as build_sh_file2:
    print("---------------------------------------------------------edit build.sh")
    build_sh_file2.write(build_sh_text_new)
  
# -------------------先运行脚本拉取资源，然后注释拉取资源，利用本地 localization_module的文件继续编译-------------------------------





# --------------------------------------------------
src_localization_repo_dir=ws_root+"/localization"
pack_res_src_conf_dir=src_localization_repo_dir+                         "/conf"
pack_res_src_inc_dir =src_localization_repo_dir+"/src/cosine_ws/install"+"/include/localization_module"
pack_res_src_lib_dir =src_localization_repo_dir+"/src/cosine_ws/install"+"/lib"

arch="aarch64"
commit_id="ea440c6d8e4a5dc9a325301ba52240bf354cdd9b"
sud_all_ver = "1221"




# --------------------------------------------------






# --------------------------------------------------
localization_inc_algso="/localization/include"
localization_lib_algso="/localization/lib"
for release_type in release_types:
  print(release_type)
  localization_build_type="-r"
  cosine_perception_build_dir_release_type_str="Release"
  if release_type == "debug":
    localization_build_type="-b"
    cosine_perception_build_dir_release_type_str="Debug"

  # os.system('cd %s/src/cosine_ws && ./compile.sh %s localization_module'%(src_localization_repo_dir,localization_build_type))
  os.system('cd %s/src/cosine_ws && ./compile.sh -c && ./compile.sh %s localization_module'%(src_localization_repo_dir,localization_build_type))










  os.system('rm -rf %s/algso%s/*'%(cosine_perception_repo_dir,localization_inc_algso))
  os.system('cp -rf %s/src/cosine_ws/install/include/localization_module/* %s/algso%s'%(src_localization_repo_dir,cosine_perception_repo_dir,localization_inc_algso))
  os.system('rm -rf %s/algso%s/liblocalization_module.so'%(cosine_perception_repo_dir,localization_lib_algso))
  os.system('cp -rf %s/src/cosine_ws/install/lib/liblocalization_module.so %s/algso%s'%(src_localization_repo_dir,cosine_perception_repo_dir,localization_lib_algso))
  # os.system('\
  #           cd %s && \
  #           ./build.sh make %s %s'%(\
  #             cosine_perception_repo_dir,\
  #             arch_type,release_type))
  # os.system('\
  #           cd %s && \
  #           rm -rf build_%s_%s/* && \
  #           ./build.sh make %s %s'%(\
  #             cosine_perception_repo_dir,\
  #             arch_type,cosine_perception_build_dir_release_type_str,\
  #             arch_type,release_type))







  
  hour_int=int(datetime.datetime.now().strftime('%H'))+8
  ver_name="localization-localization-"+datetime.datetime.now().strftime('%Y%m%d.')[2:]+str(hour_int)+datetime.datetime.now().strftime('%M')+"-"+arch+"-"+release_type+"-"+sud_all_ver+"-"+commit_id
  print(ver_name)
  pakcages_root_dir=ws_root+"/packages"
  package_root_dir=pakcages_root_dir+"/"+ver_name
  print("[time]", datetime.datetime.now().strftime('%Y%m%d.%H%M')[2:],
                "[line]", sys._getframe().f_lineno,
                "[file]", os.path.abspath(__file__)[os.path.abspath(__file__).rfind("/") + 1:],
                "ver_name is ", ver_name
                )
  if os.path.exists(package_root_dir):
    print ("[line]", sys._getframe().f_lineno,"\n",
          " package dir 已经存在 删除 新建 !!!!!!!!!"
        )
    os.system("rm -rf %s"%(package_root_dir))

  os.makedirs(package_root_dir)
  os.makedirs(package_root_dir+"/conf")
  os.makedirs(package_root_dir+"/include")
  os.makedirs(package_root_dir+"/lib")

  
  os.system("cp -rf %s %s"%(pack_res_src_conf_dir+"/"+"*", package_root_dir+"/"+"conf"))
  os.system("cp -rf %s %s"%(pack_res_src_inc_dir+"/"+"*", package_root_dir+"/"+"include"))
  os.system("cp -rf %s %s"%(pack_res_src_lib_dir+"/"+"*", package_root_dir+"/"+"lib"))

  os.system("cd %s && tar -czf %s.tar.gz %s"%(pakcages_root_dir, ver_name, ver_name))
# --------------------------------------------------